package com.cfjjc.gz.usraccount.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

//import com.cfjjc.gz.accessctl.service.D_PWD_POLICYService;
//import com.cfjjc.gz.accessctl.vo.D_PWD_POLICYVO;
import com.cfjjc.gz.common.action.PagedAction;
import com.cfjjc.gz.common.cache.CacheUtil;
import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.consts.TypeConst;
import com.cfjjc.gz.common.consts.TypeValueConst;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.util.DateTimeUtil;
import com.cfjjc.gz.common.util.StringUtil;
import com.cfjjc.gz.common.util.TreeConfig;
import com.cfjjc.gz.funcpermission.service.R_USR_ROLEService;
import com.cfjjc.gz.funcpermission.vo.R_USR_ROLEVO;
import com.cfjjc.gz.usraccount.service.A_ORGService;
import com.cfjjc.gz.usraccount.service.A_USRService;
import com.cfjjc.gz.usraccount.vo.A_ORGVO;
import com.cfjjc.gz.usraccount.vo.A_USRVO;
/*import com.cfjjc.gz.usrcredence.service.B_USR_CREDENCEService;*/

/**
 * 
 * 说明：对 用户信息表 进行增删改查的前端处理类
 * 
 * @author liuxun
 * @since 2012-03-06
 * @version 1.0
 */
public class A_USRAction extends PagedAction {

	private A_USRVO model = new A_USRVO();

	private A_ORGVO org = new A_ORGVO();
	
//	private D_PWD_POLICYVO d_pwd_policy = new D_PWD_POLICYVO();

	private TreeConfig tree = new TreeConfig();

	@Autowired
	private A_USRService a_USRService;

	@Autowired
	private A_ORGService a_ORGService;

	@Autowired
	private R_USR_ROLEService r_USR_ROLEService;

/*	@Autowired
	private D_PWD_POLICYService d_PWD_POLICYService;
	
	@Autowired
	private B_USR_CREDENCEService b_USR_CREDENCEService;*/

	public A_USRAction() {
		super();
		this.setQUERY_NAME("browseA_USRList");
	}

	// 显示用户信息表管理首页
	public String showIndex() throws Exception {
		return INDEX;
	}
	
	public String showIndexView() throws Exception {
		return "index1";
	}
	
	public String showIndexPrizes() throws Exception {
		return "index2";
	}
	
	public String showIndexDoPrizes() throws Exception {
		return "index3";
	}
	


	// 显示用户信息表列表页面
	public String queryList() throws Exception {
		preparePagedList(QUERY_NAME, getModel());
		if (!pagedRecords.exists()) {
			try {
				a_USRService.queryA_USRs((A_USRVO) pagedRecords.getParam(),
						pagedRecords);
			} catch (OTSException e) {
				this.addActionError(e.getMessage());
				return ERROR;
			}
		}
		return LIST;
	}
	
	// 显示用户信息表列表页面
	public String queryList_RSC_USR() throws Exception {
		preparePagedList("query_rsc_usr", getModel());
		if (!pagedRecords.exists()) {
			try {
				a_USRService.queryA_USRs((A_USRVO) pagedRecords.getParam(),
						pagedRecords);
			} catch (OTSException e) {
				this.addActionError(e.getMessage());
				return ERROR;
			}
		}
		return LIST;
	}

	// 显示机构信息下的用户树
	public String showTree() throws Exception {
		if (SystemConst.YES_CONST.equals(model.getIs_monitor())) {
			tree.setUrl("A_USR!queryMonitorTreeNode.do");
		} else {
			tree.setUrl("A_USR!queryTreeNode.do");
		}
		return TREE;
	}

	// 显示机构信息下的会话用户树
	public String queryMonitorTreeNode() throws Exception {
		
		Map root = null;

		// 显示一级节点时，自动添加根节点
		root = new HashMap();
		root.put("id", "000");
		root.put("text", "在线用户监控");
		root.put("attributes", "");

		List<A_USRVO> usrs = null;

		model.setIs_monitor(SystemConst.YES_CONST);
		usrs = a_USRService.queryMonitorA_USRList(model);

		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();		

		for (A_USRVO node : usrs) {
			Map<String, Object> item = new HashMap<String, Object>();

			item.put("id", node.getUsr_id());
			item.put("text", node.getLogin_name() + " " + node.getUsr_name());
			item.put("attributes", node);
			item.put("state", "open");
			items.add(item);
		}

		this.rows.add(root);
		root.put("children", items);
		
		return TREE_NODE;
	}

	// 显示机构信息下的用户树
	public String queryTreeNode() throws Exception {

		Map root = null;

		// 显示一级节点时，自动添加根节点
		if (id == null || id.length == 0) {
			A_ORGVO query_root = new A_ORGVO();
			query_root.setOrg_code("1002");
			query_root.setOrg_status(TypeValueConst.ORG_STATUS_NORMAL);
			A_ORGVO res_root = a_ORGService.queryA_ORG(query_root);

			root = new HashMap();
			root.put("id", res_root.getOrg_code());
			root.put("text", res_root.getOrg_name());
			root.put("attributes", res_root);

			org.setUpper_org_code("1002");
		} else {
			org.setUpper_org_code(id[0]);
		}

		List<A_ORGVO> nodes = null;

		try {
			org.setOrg_status(TypeValueConst.ORG_STATUS_NORMAL);
			nodes = a_ORGService.queryA_ORGList(org);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}

		List<A_USRVO> usrs = null;

		try {
			A_ORGVO query_org = new A_ORGVO();
			query_org.setOrg_code(org.getUpper_org_code());
			query_org.setOrg_status(TypeValueConst.ORG_STATUS_NORMAL);
			A_ORGVO res_org = a_ORGService.queryA_ORG(query_org);
			if (res_org != null) {
				model.setOrg_id(res_org.getOrg_id());
				model.setUsr_status(TypeValueConst.USR_STATUS_NORMAL);
				usrs = a_USRService.queryA_USRList(model);
			}
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}

		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
		for (A_ORGVO node : nodes) {
			Map<String, Object> item = new HashMap<String, Object>();

			item.put("id", node.getOrg_code());
			item.put("text", node.getOrg_code() + " " + node.getOrg_name());
			item.put("attributes", node);
			// if ("0".equals(node.getIs_leaf())) {
			item.put("state", "closed");
			// }
			items.add(item);
		}

		for (A_USRVO node : usrs) {
			Map<String, Object> item = new HashMap<String, Object>();

			item.put("id", node.getUsr_id());
			item.put("text", node.getLogin_name() + " " + node.getUsr_name());
			item.put("attributes", node);
			item.put("state", "open");
			items.add(item);
		}

		if (root != null) {
			root.put("children", items);
			this.rows.add(root);
		} else {
			this.rows = items;
		}

		return TREE_NODE;
	}

	// 显示选择用户信息页面
	public String showSelect() throws Exception {
		return SELECT;
	}

	// 显示用户信息表的明细页面
	public String showInfo() throws Exception {
		try {
			if (StringUtil.isEmpty(model.getKey())) {
				return INFO;
			}

			A_USRVO param = new A_USRVO();
			param.setKey(model.getKey());
			model = a_USRService.queryA_USR(param);

			R_USR_ROLEVO usr_role = new R_USR_ROLEVO();
			usr_role.setUsr_id(model.getUsr_id());
			rows = this.r_USR_ROLEService.querySelectedRoleList(usr_role);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR_PAGE;
		}
		return INFO;
	}

	// 显示用户信息表录入页面
	public String showAdd() throws Exception {
		this.model.setEffect_time(DateTimeUtil.getDateTime());
		setOperationType(SystemConst.OPERATION_TYPE_NEW);
		return INPUT;
	}

	// 显示修改密码
 	public String showUpdatePwdIndex() {
/*		D_PWD_POLICYVO query = new D_PWD_POLICYVO();
		query.setPwd_type(TypeValueConst.USR_PWD_TYPE);
		try {
			this.d_pwd_policy = d_PWD_POLICYService.queryD_PWD_POLICY(query);
		} catch (OTSException e) {
			e.printStackTrace();
		}

		
		Map pwd_seed_map = b_USR_CREDENCEService.queryPwdSeed();
		model.setLower_seed(pwd_seed_map.get("lower_seed").toString());
		model.setUpper_seed(pwd_seed_map.get("upper_seed").toString());
		model.setNumber_seed(pwd_seed_map.get("number_seed").toString());
		model.setSpecial_seed(pwd_seed_map.get("special_seed").toString());*/

		A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
		model.setUsr_id(usr.getUsr_id());
		return "updatePwd";
	} 

	// 自服务我的信息维护首页
	public String showUpdateInformation() {

		if (StringUtil.isEmpty(model.getUsr_id())) {
			A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
			A_USRVO query = new A_USRVO();
			query.setUsr_id(usr.getUsr_id());
			try {
				model = a_USRService.queryA_USR(query);
				
				if (!StringUtil.isNotEmpty(model.getUsr_type())) {
					Map<String,String> map = new HashMap<String,String>();
					map.put("type_code", TypeConst.USR_TYPE);
					map.put("type_value_code", model.getUsr_type());
					model.setUsr_type_decode((String) CacheUtil.getProperty("P_TYPE_VALUE", "type_value_name", map));
				}
			} catch (OTSException e) {
				this.addActionError(e.getMessage());
				return ERROR_PAGE;
			}
		}
		setOperationType(SystemConst.OPERATION_TYPE_MODIFY);
		return "updateInformation";
	}
	
	// 自服务我的信息维护保存处理
	public String doUpdateInformation() throws Exception {
		try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setUpdate_by(usr.getUsr_id());
			a_USRService.doUpdateInformation(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return "updateInformation";
	}

	// 保存修改修改密码
	public String doUpdatePwd() {
		try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setUpdate_by(usr.getUsr_id());
			a_USRService.doUpdateA_USR_pwd(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return "updatePwd";
	}

	// 保存用户信息表录入
	public String doInsert() throws Exception {
		try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setCreate_by(usr.getUsr_id());
			model.setNode_id(SystemConst.NODE_ID);
			a_USRService.doInsertA_USR(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}

	// 显示修改页面
	public String showEdit() throws Exception {
		try {
			if (StringUtil.isEmpty(model.getKey())) {
				return INPUT;
			}

			A_USRVO param = new A_USRVO();
			param.setKey(model.getKey());
			model = a_USRService.queryA_USR(param);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR_PAGE;
		}

		setOperationType(SystemConst.OPERATION_TYPE_MODIFY);
		return INPUT;
	}

	// 保存对用户信息表的修改
	public String doUpdate() throws Exception {
		try {
			A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setUpdate_by(usr.getUsr_id());
			a_USRService.doUpdateA_USR(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}

	// 删除用户信息表。允许批量删除。允许部分删除成功
	public String doDelete() throws Exception {
		try {
			A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
			for (int n = 0; n < id.length; n++) {
				A_USRVO param = new A_USRVO();
				param.setKey(id[n]);
				param.setUpdate_by(usr.getUsr_id());
				a_USRService.doDeleteA_USR(param);
			}
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}

	// 保存用户授权时间策略
	public String doUpdateA_USRbyTimePolicy() throws Exception {
		try {
			a_USRService.doUpdateA_USRbyTimePolicy(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}

	// 删除用户授权时间策略
	public String deleteTimePolicyOperation() throws Exception {
		try {
			a_USRService.doDeleteTimePolicyOperation(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}

	/** 以下为set,get方法 */
	public Object getModel() {
		return model;
	}

	public TreeConfig getTree() {
		return tree;
	}

	/**
	 * @return 返回d_pwd_policy
	 */
/*	public D_PWD_POLICYVO getD_pwd_policy() {
		return d_pwd_policy;
	}

	*//**
	 * @param d_pwd_policy 
	 *            要设置的d_pwd_policy 
	 *//*
	public void setD_pwd_policy(D_PWD_POLICYVO d_pwd_policy) {
		this.d_pwd_policy = d_pwd_policy;
	}*/

	public void setOrg(A_ORGVO org) {
		this.org = org;
	}
}
